	package manong.controller;

import com.github.pagehelper.PageInfo;
import manong.constant.LayerResponse;
import manong.constant.ServerResponse;
import manong.beans.User;
import manong.service.IUserService;
import manong.util.ToLayerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

   
    @RequestMapping("add")
    public ServerResponse<String> add(String username, String password, String role, String mark) {
        return iUserService.add(username, password, role,mark);

    }

    @RequestMapping("login")
    public ServerResponse<User> login(String username, String password, String role, HttpSession session) {
        //根据登录用户的身份来进行查询，如：超级管理、教师、学生
        ServerResponse<User> login = iUserService.login(username, password, role);
        if (login.isSuccess()) {
            //查询成功，将用户信息存入session域当中，为之后操作方便数据的提供
            session.setAttribute("user", login.getData());
            System.out.println(session.getId());
        }
        return login;
    }

  
    @RequestMapping("del")
    public ServerResponse<String> del(Integer id) {
        return iUserService.del(id);
    }

   
    @RequestMapping("findAll")
    public ServerResponse<PageInfo<User>> findAll(HttpSession session, Integer pageNum, Integer pageSize) {
        User user = (User) session.getAttribute(session.getId());
        ServerResponse<PageInfo<User>> all = iUserService.findAll("超级管理员", pageNum, pageSize);
        return all;
    }

    
    @RequestMapping("find")
    public ServerResponse<User> find(Integer id) {
        return iUserService.findById(id);
    }

 
    @RequestMapping("update")
    public ServerResponse<String> update(Integer id, String username, String password,String role,String mark) {
        return iUserService.update(id, username, password,role,mark);
    }
}
